<?php
namespace App\Api\Fcjd;

use PhalApi\Api;

use function PHPSTORM_META\type;

/**
 * 获取设备，多参数
 * @author dogstar 20200331
 */
class Device extends Api {
    public function getRules() {
        return array(
            'getList' => array(
                'username' => array('name' => 'username', 'require' => true, 'min' => 1, 'max' => 50, 'desc' => 'username'),
                'page' => array('name' => 'page', 'type' => 'int','min' => 1,'require' => true, 'desc' => '页码'),
                'size' => array('name' => 'size', 'type' => 'int','min' => 1, 'require' => true, 'desc' => '每页数量'),
                'json' => array('name' => 'json', 'require' => false, 'desc' => '条件'),
            ),
        );
    }

    /**
     * 获取设备
     * @desc 更新帐号数据
     * @return int res 是否更新成功
     */
    public function getList() {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *,x-token,X-token');
        
        $username = $this->username;
        $page = $this->page;
        $size = $this->size;
        $json = $this->json;

        $where = array(
            'username' => $username,
        );

        if( $json ){
            $json = '%' . $json . '%';
            $where = array(
                'username' => $username,
                '(mobile LIKE? OR name LIKE? OR mun LIKE? OR ip LIKE?)' => array($json, $json, $json, $json),
            );
        }

        // return $where;
 
        $total = \PhalApi\DI()->notorm->phone_list->where($where)->count();
        $items = \PhalApi\DI()->notorm->phone_list
            ->where($where)
            ->select('id,ip,mun,name,mobile,username,oaid,task,task2,task_date,task_day,time_line,update_time,alipay_name,alipay_account')
            ->limit(($page - 1) * $size, $size)
            ->order('time_line Asc')
            ->fetchAll();
        if( $items ){
            return array(
                'count' => $total,
                'data' => $items,
            );
        }
    }

}
